﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace Homework8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private bool isDigit = false;
        private int prenumber;
        private string oper;
        private Stack num = new Stack();
        private Stack op = new Stack();

        private void enterStack(String mode)
        {
            if (mode == "number")
            {
                num.Push(prenumber);
            }
            if (mode == "operation")
            {
                op.Push(oper);
            }

        }
        private void button_digit_Pressed(Button bt)
        {
            if (!isDigit)
            {
                prenumber = int.Parse(textBox1.Text);
                enterStack("number");
                textBox1.Text = bt.Text;
                oper = bt.Text;
                enterStack("operation");

            }
            else
            {
                textBox1.Text = bt.Text;
            }

        }

        //result button
        private void button15_Click(object sender, EventArgs e)
        {
            isDigit = false;
            button_digit_Pressed(button15);
            op.Pop();

            double result = 0;
            int number2 = Convert.ToInt32(num.Pop());
            int number1 = Convert.ToInt32(num.Pop());

            String op0 =
               Convert.ToString(op.Pop());
           
            switch (op0)
            {
                case "+":
                    result = number1 + number2;
                    break;
                case "-":
                    result = number1 - number2;
                    break;
                case "×":
                    result = number1 * number2;
                    break;
                case "÷":
                    result = Convert.ToDouble( number1 ) / Convert.ToDouble(number2);
                    break;

            }
            textBox1.Text = Convert.ToString(number1) + op0 + Convert.ToString(number2) + "=" + Convert.ToString(result);
        }

        //clear button
        private void button13_Click(object sender, EventArgs e)
        {
            textBox1.Text = " ";
        }

        //number button
        private void button1_Click(object sender, EventArgs e)
        {
            isDigit = true;
            button_digit_Pressed(button1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            isDigit = true;
            button_digit_Pressed(button2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            isDigit = true;
            button_digit_Pressed(button3);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            isDigit = true;
            button_digit_Pressed(button5);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            isDigit = true;
            button_digit_Pressed(button6);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            isDigit = true;
            button_digit_Pressed(button7);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            isDigit = true;
            button_digit_Pressed(button9);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            isDigit = true;
            button_digit_Pressed(button10);

        }

        private void button11_Click(object sender, EventArgs e)
        {
            isDigit = true;
            button_digit_Pressed(button11);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            isDigit = true;
            button_digit_Pressed(button14);
        }

        //operation button
        private void button12_Click(object sender, EventArgs e)
        {
            isDigit = false;
            button_digit_Pressed(button12);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            isDigit = false;
            button_digit_Pressed(button16);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            isDigit = false;
            button_digit_Pressed(button4);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            isDigit = false;
            button_digit_Pressed(button8);
        }


    }
}
